package com.abc.recycling.service.impl;

import com.abc.recycling.model.RecyclingStation;
import com.abc.recycling.service.MapService;
import com.abc.recycling.service.StationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class StationServiceImpl implements StationService {

    @Autowired
    private MapService mapService;

    @Override
    public List<RecyclingStation> getNearbyStations(Double latitude, Double longitude, Integer radius) {
        try {
            return mapService.getNearbyStations(latitude, longitude, radius);
        } catch (Exception e) {
            // 如果API调用失败，返回一些测试数据
            List<RecyclingStation> stations = new ArrayList<>();
            
            // 添加一些测试数据
            RecyclingStation station1 = new RecyclingStation();
            station1.setId(1L);
            station1.setName("测试回收站1");
            station1.setAddress("测试地址1");
            station1.setLatitude(latitude + 0.01);
            station1.setLongitude(longitude + 0.01);
            station1.setBusinessHours("09:00-18:00");
            station1.setContactPhone("13800138001");
            station1.setStatus(1);
            stations.add(station1);

            RecyclingStation station2 = new RecyclingStation();
            station2.setId(2L);
            station2.setName("测试回收站2");
            station2.setAddress("测试地址2");
            station2.setLatitude(latitude - 0.01);
            station2.setLongitude(longitude - 0.01);
            station2.setBusinessHours("08:00-17:00");
            station2.setContactPhone("13800138002");
            station2.setStatus(1);
            stations.add(station2);

            return stations;
        }
    }

    @Override
    public List<RecyclingStation> getAllStations() {
        // 返回所有回收站数据
        List<RecyclingStation> stations = new ArrayList<>();
        
        // 添加一些测试数据
        RecyclingStation station1 = new RecyclingStation();
        station1.setId(1L);
        station1.setName("回收站1");
        station1.setAddress("北京市海淀区XX路XX号");
        station1.setLatitude(39.925);
        station1.setLongitude(116.414);
        station1.setBusinessHours("09:00-18:00");
        station1.setContactPhone("13800138001");
        station1.setStatus(1);
        stations.add(station1);

        RecyclingStation station2 = new RecyclingStation();
        station2.setId(2L);
        station2.setName("回收站2");
        station2.setAddress("北京市朝阳区XX路XX号");
        station2.setLatitude(39.915);
        station2.setLongitude(116.404);
        station2.setBusinessHours("08:00-17:00");
        station2.setContactPhone("13800138002");
        station2.setStatus(1);
        stations.add(station2);

        return stations;
    }

    @Override
    public List<RecyclingStation> searchStations(String keyword) {
        // 根据关键词搜索回收站
        List<RecyclingStation> allStations = getAllStations();
        List<RecyclingStation> result = new ArrayList<>();
        
        for (RecyclingStation station : allStations) {
            if (station.getName().contains(keyword) || 
                station.getAddress().contains(keyword)) {
                result.add(station);
            }
        }
        
        return result;
    }
} 